package com.zhaoxi.project.fms.service.impl;

import java.util.List;

import com.zhaoxi.common.utils.Arith;
import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.framework.web.server.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhaoxi.project.fms.mapper.FmsAccountMapper;
import com.zhaoxi.project.fms.domain.FmsAccount;

/**
 * 账户信息Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-08-14
 */
@Service
public class FmsAccountServiceImpl extends BaseServiceImpl {
    @Autowired
    private FmsAccountMapper fmsAccountMapper;

    /**
     * 查询账户信息
     *
     * @param id 账户信息主键
     * @return 账户信息
     */

    public FmsAccount selectFmsAccountById(Long id) {
        return fmsAccountMapper.selectFmsAccountById(id);
    }

    /**
     * 查询账户信息列表
     *
     * @param fmsAccount 账户信息
     * @return 账户信息
     */

    public List<FmsAccount> selectFmsAccountList(FmsAccount fmsAccount) {
        return fmsAccountMapper.selectFmsAccountList(fmsAccount);
    }

    /**
     * 新增账户信息
     *
     * @param fmsAccount 账户信息
     * @return 结果
     */

    public int insertFmsAccount(FmsAccount fmsAccount) {
        return fmsAccountMapper.insertFmsAccount(fmsAccount);
    }

    /**
     * 修改账户信息
     *
     * @param fmsAccount 账户信息
     * @return 结果
     */

    public int updateFmsAccount(FmsAccount fmsAccount) {
        fmsAccount.setUpdateTime(DateUtils.getNowDate());
        return fmsAccountMapper.updateFmsAccount(fmsAccount);
    }

    /**
     * 批量删除账户信息
     *
     * @param ids 需要删除的账户信息主键
     * @return 结果
     */

    public int deleteFmsAccountByIds(Long[] ids) {
        return fmsAccountMapper.deleteFmsAccountByIds(ids);
    }

    /**
     * 删除账户信息信息
     *
     * @param id 账户信息主键
     * @return 结果
     */

    public int deleteFmsAccountById(Long id) {
        return fmsAccountMapper.deleteFmsAccountById(id);
    }

    /**
     * 删除账户信息信息
     *
     * @param fmsAccount 账户信息
     * @return 结果
     */
    public int updateFmsAccountCurrentAmountById(FmsAccount fmsAccount) {
        Double money = fmsAccount.getParamsToDouble("money");
        if (money == null){
            return -1;
        }
        fmsAccount.setParams("before",fmsAccount.getCurrentAmount());
        fmsAccount.setParams("currentAmount", Arith.add(fmsAccount.getCurrentAmount(),money));
        this.insertFmsAccountLog(fmsAccount);

        fmsAccount.setCurrentAmount(money);
        fmsAccount.setUpdateBy(this.getUsername());
        fmsAccount.setUpdateTime(DateUtils.getNowDate());
        return fmsAccountMapper.updateFmsAccountCurrentAmountById(fmsAccount);
    }

    private int insertFmsAccountLog(FmsAccount fmsAccount) {

        fmsAccount.setCreateBy(this.getUsername());
        fmsAccount.setCreateTime(DateUtils.getNowDate());
        fmsAccount.setTenantId(this.getTenantId());
        fmsAccount.setUserId(this.getUserId());
        return fmsAccountMapper.insertFmsAccountLog(fmsAccount);
    }

}
